package com.example.covid19.controller;


import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.Dto.Editdto;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Enum.MessageTemplate;
import com.example.covid19.entity.StationMessage;
import com.example.covid19.mapper.StationMessageMapper;
import com.example.covid19.service.StationMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2023-05-10
 */
@RestController
@RequestMapping("/station-message")
@Component
public class StationMessageController {
    @Autowired
    StationMessageService stationMessageService;
    private final StationMessageMapper stationMessageMapper;

    public StationMessageController(StationMessageMapper stationMessageMapper) {
        this.stationMessageMapper = stationMessageMapper;
    }

    /**
     *  发送站内信方法
     * @param
     * @param
     * @return
     */
    @RequestMapping("/test")
    public Result stuexamine(){
        StationMessage stationMessage = new StationMessage();
        stationMessage.setStudentid(Integer.valueOf("0917420002"));
        stationMessage.setTitle("dsds");
        stationMessage.setContent("dsds");
        stationMessage.setTimeer(LocalDateTime.now());
        stationMessage.setState(0);
        stationMessage.setTemplate(0);
        stationMessage.setUid("fd");
        stationMessage.setResult(0);
        stationMessage.setAuid(00);
        stationMessage.setExpirationTime(LocalDate.now());
        stationMessageService.save(stationMessage);
        return Result.success(200,"s",null);
    }

    /**
     *
     * @param editdto
     * @return 获取当前用户的站内信信息,ne8不看到管理员的消息
     */
    @RequestMapping("/getmessage")
    public Result getmessage(@RequestBody Editdto editdto){
        return Result.success(200,"成功",stationMessageService.
                list(new QueryWrapper<StationMessage>().eq("studentid",editdto.getUserid()).ne("template",8)));
    }
    /**
     *
     * @param
     * @return 获取当前管理员的站内信信息
     */
    @RequestMapping("/getglmessage")
    public Result getglmessage(HttpServletRequest httpServletRequest){
        String s = (String) StpUtil.getLoginIdByToken(httpServletRequest.getHeader("satoken"));
        return Result.success(200,"成功",stationMessageService.list(new QueryWrapper<StationMessage>().eq("auid",s)));
    }

    /**
     * 将已查看的消息取消未读标识
     */
    @RequestMapping("/getstate")
    public Result getstate(@RequestBody Editdto editdto){
        StationMessage stationMessage = stationMessageService.getOne(new QueryWrapper<StationMessage>().eq("id",editdto.getUserid()));
        stationMessage.setState(1);
        stationMessageService.updateById(stationMessage);
        return Result.success(200,"ok",null);
    }


    /**
     *
     * @param editdto
     * @return 查看还有多少条信息没读
     */
    @RequestMapping("/getcounter")
    public Result getcounter(@RequestBody Editdto editdto){
        return Result.success(200,"ok",stationMessageMapper.getcount(editdto.getUserid()));
    }

    /**
     *
     * @param
     * @return 查看管理员有多少条消息没读
     */
    @RequestMapping("/getglcounter")
    public Result getglcounter(HttpServletRequest httpServletRequest){
        String s = (String) StpUtil.getLoginIdByToken(httpServletRequest.getHeader("satoken"));
        return Result.success(200,"ok",stationMessageMapper.getglcount(s));
    }
}
